package com.groupfifteen.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import com.groupfifteen.entity.SysStudentScoreEntity;

import java.util.List;

@Mapper
public interface SysStudentScoreDao extends BaseMapper<SysStudentScoreEntity> {
    /**
     * 添加学生成绩
     *
     * @param score 学生成绩
     * @return 考试成绩
     */
    @Options(useGeneratedKeys = true, keyProperty = "scoreId")
    @Insert("insert into student_score(exam_code,student_id,subject,pt_score,et_score,score,answer_date) values(#{examCode}," +
            "#{studentId},#{subject},#{ptScore},#{etScore},#{score},#{answerDate})")
    Integer add(SysStudentScoreEntity score);

    /**
     * 查询所有考试成绩
     *
     * @return 考试成绩
     */
    @Select("select score_id,exam_code,student_id,subject,pt_score,et_score,score,answer_date from student_score order by score_id " +
            "desc")
    List<SysStudentScoreEntity> findAll();

    /**
     * 查询考试成绩分页
     *
     * @param page      分页对象
     * @param studentId 学生id
     * @return 考试成绩
     */
    @Select("select score_id,exam_code,student_id,subject,pt_score,et_score,score,answer_date from student_score where student_id = " +
            "#{studentId} order by score_id asc")
    IPage<SysStudentScoreEntity> findById(Page<?> page, @Param("studentId") Integer studentId);

    /**
     * 通过考试编号 查询学生成绩
     *
     * @param examCode 考试编号
     * @return 考试成绩
     */
    @Select("select max(et_score) as et_score from student_score where exam_code = #{examCode} group by student_id")
    List<SysStudentScoreEntity> findByExamCode(Integer examCode);
}
